package com.kanshu.ksgb.fastread.doudou.common.business.ad

import android.app.Activity
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.hz.yl.b.HHType
import com.hz.yl.b.HhInfo
import com.hz.yl.b.mian.*
import com.kanshu.ksgb.fastread.doudou.R
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter
import com.kanshu.ksgb.fastread.doudou.common.business.ad.retrofit.ADConfigBean
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader
import com.kanshu.ksgb.fastread.doudou.common.util.Utils


/**
 * Title:
 * Description:
 * Copyright: Copyright(c)2018
 * Company: 看书网
 * CreateTime: 2018/8/7 9:45
 *
 * @author EvanHe
 * @version 3.0
 */
class LuoMiUtils {

    companion object {
        fun fetchSplashAd(activity: Activity, container: ViewGroup?, skipContainer: View?, adConfig: ADConfigBean, adListener: BaseAdListener?) {
            val adContainer = container ?: FrameLayout(activity)
            skipContainer?.visibility = View.GONE
            if (adListener is SplashAdListener) {
                adListener.isShowSkipView(false)
                adListener.onSplashHolder(false)
            }
            //洛米广告没有成功回调，所以闪屏广告返回失败
            if (adConfig.ad_position?.toInt() == ADConfigBean.POSITION_SPACE_TIME_SPLASH) {
                adListener?.onAdLoadFailed()
                return
            }
            HaSplash(activity, adContainer, null, object : SplashListener {

                override fun onPresent() {
                    Log.i("LuoMi_Splash", "onPresent")
                    adListener?.onADClosed()
                    AdUtils.pVUVAd(AdPresenter.AD_SHOW, adConfig)
                }

                override fun onNoAD(errorInfo: String?) {
                    Log.i("LuoMi_Splash", "onNoAD:  $errorInfo")
                    adListener?.onAdLoadFailed()
                }

                override fun OnClicked() {
                    Log.i("LuoMi_Splash", "onClicked")
                    adListener?.onAdClicked()
                    AdUtils.pVUVAd(AdPresenter.AD_CLICK, adConfig)
                }
            }, true)
        }

        private var mCenBanner: CenBanner? = null
        fun fetchBannerAd(activity: Activity, container: ViewGroup?, adConfig: ADConfigBean, adStyle: Int, layout: Int, adListener: BaseAdListener?) {
            val adContainer = container ?: FrameLayout(activity)
            mCenBanner?.onPause(activity)
            mCenBanner?.onDestory(activity)
            mCenBanner = null
            mCenBanner = CenBanner(activity.applicationContext, adContainer, object : CenBannerListener {
                override fun onSuccess() {
                    Log.i("LuoMi_Banner", "onSuccess")
                    //返回banner广告对象来判断在阅读器中是否限制高度
                    mCenBanner?.let {
                        adListener?.onBackAd(it)
                        adContainer.tag = it
                    }
                    adListener?.onAdLoadSucceeded(adContainer)
                    AdUtils.pVUVAd(AdPresenter.AD_SHOW, adConfig)
                }

                override fun onPause() {
                    Log.i("LuoMi_Banner", "onPause")
                }

                override fun onClicked() {
                    Log.i("LuoMi_Banner", "onClicked")
                    adListener?.onAdClicked()
                    AdUtils.pVUVAd(AdPresenter.AD_CLICK, adConfig)
                }

                override fun onNoAd() {
                    Log.i("LuoMi_Banner", "onNoAd")
                    AdUtils.fetchAdFailed(activity, adContainer, adConfig, adStyle, layout, adListener)
                }

                override fun onError() {
                    Log.i("LuoMi_Banner", "onError")
                    AdUtils.fetchAdFailed(activity, adContainer, adConfig, adStyle, layout, adListener)
                }
            }, HHType.SMALL_BANNER, true)
            mCenBanner?.onResume(activity)
        }

        /**
         * 上文下大图广告
         */
        private var mCenBigBanner: CenBanner? = null

        fun fetchBigBannerAd(activity: Activity, container: ViewGroup?, adConfig: ADConfigBean, adStyle: Int, layout: Int, adListener: BaseAdListener?) {
            val adContainer = container ?: FrameLayout(activity)
            mCenBigBanner?.onPause(activity)
            mCenBigBanner?.onDestory(activity)
            mCenBigBanner = null
            mCenBigBanner = CenBanner(activity.applicationContext, adContainer, object : CenBannerListener {
                override fun onSuccess() {
                    Log.i("LuoMi_big_Banner", "onSuccess")
                    //返回banner广告对象来判断在阅读器中是否限制高度
                    mCenBigBanner?.let {
                        adListener?.onBackAd(it)
                        adContainer.tag = it
                    }
                    adListener?.onAdLoadSucceeded(adContainer)
                    AdUtils.pVUVAd(AdPresenter.AD_SHOW, adConfig)
                }

                override fun onPause() {
                    Log.i("LuoMi_big_Banner", "onPause")
                }

                override fun onClicked() {
                    Log.i("LuoMi_big_Banner", "onClicked")
                    adListener?.onAdClicked()
                    AdUtils.pVUVAd(AdPresenter.AD_CLICK, adConfig)
                }

                override fun onNoAd() {
                    Log.i("LuoMi_big_Banner", "onNoAd")
                    AdUtils.fetchAdFailed(activity, adContainer, adConfig, adStyle, layout, adListener)
                }

                override fun onError() {
                    Log.i("LuoMi_big_Banner", "onError")
                    AdUtils.fetchAdFailed(activity, adContainer, adConfig, adStyle, layout, adListener)
                }
            }, HHType.BIG_BANNER, true)
            mCenBigBanner?.onResume(activity)
        }

        fun fetchNativeAd(activity: Activity, container: ViewGroup?, adConfig: ADConfigBean, adStyle: Int, layout: Int, adListener: BaseAdListener?) {
            val adContainer = container ?: FrameLayout(activity)
            val type = "35" // 设置图片比例为3：4 （HHType.bookshelf）
            HmNative(activity.applicationContext, type, object : NativeListener {
                override fun LoadSuccess(ads: MutableList<HhInfo>?) {
                    Log.i("LuoMi_Native", "LoadSuccess  size: ${ads?.size}")
                    if (Utils.isEmptyList(ads)) {
                        AdUtils.fetchAdFailed(activity, adContainer, adConfig, adStyle, layout, adListener)
                        return
                    }
                    ads!![0].also {
                        val rootView = LayoutInflater.from(activity).inflate(layout, adContainer, false)
                        val imageView: ImageView = rootView.findViewById(R.id.cover) as ImageView
                        val title = rootView.findViewById(R.id.book_title) as TextView
                        Log.i("LuoMi_Native", "imageUrl: ${it.imgurl} title: ${it.wenzi}")
                        activity.runOnUiThread {
                            GlideImageLoader.load(it.imgurl, imageView, GlideImageLoader.getEmptyConfig())
                            title.text = it.wenzi ?: adConfig.title ?: ""
                            UpLoadPay.getInstance().upLoadNativeShow(activity, it)
                            adContainer.removeAllViews()
                            adContainer.addView(rootView)
                            adListener?.onAdLoadSucceeded(rootView)
                            imageView.setOnClickListener { _ ->
                                AdUtils.pVUVAd(AdPresenter.AD_CLICK, adConfig)
                                UpLoadPay.getInstance().upLoadNativeClick(activity, it)
                            }
                            AdUtils.pVUVAd(AdPresenter.AD_SHOW, adConfig)
                        }
                    }
                }

                override fun LoadError(errorInfo: String?) {
                    Log.i("LuoMi_Native", "onError $errorInfo")
                    AdUtils.fetchAdFailed(activity, adContainer, adConfig, adStyle, layout, adListener)
                }
            }, 1)
        }

        @JvmStatic
        fun onPauseAd(context: Context) {
            mCenBanner?.run {
                onPause(context)
            }
            mCenBigBanner?.run {
                onPause(context)
            }
        }

        @JvmStatic
        fun onResumeAd(context: Context) {
            mCenBanner?.run {
                onResume(context)
            }
            mCenBigBanner?.run {
                onResume(context)
            }
        }

        @JvmStatic
        fun onDestroyAd(context: Context) {
            mCenBanner?.onDestory(context)
            mCenBanner = null
            mCenBigBanner?.onDestory(context)
            mCenBigBanner = null
        }

    }
}